package com.ccubee.data.transfer.types;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.function.Supplier;

/**
 * 类型
 *
 * @Author ycf
 * @Date 2022/11/21 22:53
 * @Version 1.0
 */
@AllArgsConstructor
@Getter
public enum RejectedExecutionHandlerType {
    /**
     * 直接抛出异常，默认策略
     */
    AbortPolicy(ThreadPoolExecutor.AbortPolicy::new),
    /**
     * 用调用者所在的线程来执行任务
     */
    CallerRunsPolicy(ThreadPoolExecutor.CallerRunsPolicy::new),
    /**
     * 丢弃阻塞队列中靠最前的任务，并执行当前任务
     */
    DiscardOldestPolicy(ThreadPoolExecutor.DiscardOldestPolicy::new),
    /**
     * 直接丢弃任务
     */
    DiscardPolicy(ThreadPoolExecutor.DiscardPolicy::new),
    ;
    /**
     * 处理类型
     */
    private final Supplier<RejectedExecutionHandler> handler;

    /**
     * 获取类型
     *
     * @return 结果
     */
    public RejectedExecutionHandler rejected() {
        return handler.get();
    }
}
